import { defineStore, storeToRefs } from 'pinia';
import { User } from '@/store/user';
import { Local } from "@/utils/storage";
const userInfo = User();
const { roleInfo } = storeToRefs(userInfo);
import { Page } from '@/store/page';
const page = Page();
const { settings } = storeToRefs(page);
import { joinURL } from '@/utils/publicMethods'

interface ChatState {
  usesrType: any, // 角色
  requestid: any, //请求id
  todeviceid: any, //对方设备ID
  chatitemList: [], // 全部好友列表
  moneyauth: number, // 转账权限
  muteuser: number, // 禁言权限
  mutetype: any, // 禁言权限
  headPhoto: any, // 头像
  textauth: number, // 文字发送权限
  picauth: number, // 图片发送权限
  vioauth: number, // 视频发送权限
  momoauth: number, // 表情发送权限
  autoreply: boolean, // 自动回复
  autoreplytext: any, // 自动回复
  isOpenCorrect: boolean, // 成功链接并处于登录状态
  auth: any, // 新加的登录权限
}

export const Chat = defineStore('chat', {

  state: (): ChatState => ({
    usesrType: 0, // 0 普通用户；1客服；2代理；3推广
    requestid: 5000, //请求id  
    todeviceid: 2654917, //对方设备ID
    chatitemList: [], //对方设备ID
    moneyauth: 0, // 转账权限
    muteuser: 0, // 禁言权限
    mutetype: {}, // 禁言权限
    textauth: 0, // 禁言权限
    headPhoto: '', // 头像
    picauth: 0, // 禁言权限
    vioauth: 0, // 禁言权限
    momoauth: 0, // 禁言权限
    autoreply: false, // 自动回复
    autoreplytext: '', // 自动回复
    isOpenCorrect: false, // 成功链接并处于登录状态
    auth: {}, // 新加的登录权限
  }),
  actions: {
    async setIsOpenCorrect(value: boolean) {
      this.isOpenCorrect = value
    },
    async setRequestid(value: number) {
      this.requestid = value
    },
    async setTodeviceid(value: number) {
      this.todeviceid = value
    },
    async setChatitemList(value: any) {
      console.log('setChatitemList', value)
      this.chatitemList = value
    },
    async setUsesrType(value: any) {
      this.usesrType = value
    },
    async setMoneyauth(value: any) {
      this.moneyauth = value
    },
    async setMuteuser(value: any) {
      this.muteuser = value
    },
    async setAutoreply(value: boolean) {
      this.autoreply = value
    },
    async setAutoreplyText(value: any) {
      this.autoreplytext = value
    },
    async getConfig() {
      let chat_info = settings.value.chat_res?.chat_info
      if (chat_info) {
        let device_id = roleInfo.value.id || Local.get('deviceid')
        let url = joinURL(chat_info, `/api/user/info?device_id=${device_id}`)
        console.log('chat_info', url)
        // 获取角色
        // fetch(chat_info + `/api/user/info?device_id=2650684`)
        fetch(url)
          .then(response => {
            if (!response.ok) {
              throw new Error('Network response was not ok ' + response.statusText);
            }
            return response?.json(); // 将响应解析为 JSON
          })
          .then(res => {
            let data = res?.data
            console.log('角色', res)
            this.usesrType = data?.user_type || 0
            this.moneyauth = data?.moneyauth || 0
            this.muteuser = data?.muteuser || 0
            this.mutetype = data?.mutetype || 0
            this.headPhoto = data?.head_photo || ''
            Local.set('usesrType', data?.user_type || 0)
            this.auth = data?.auth || {}
            // this.picauth = data?.picauth || 0
            // this.vioauth = data?.vioauth || 0
            // this.momoauth = data?.momoauth || 0
          })
      }
    }
  }
});
